/**
 * 定义DataAnalysisController数据分析模块
 */
(function(window, angular, undefined){
	var DataAnalysisController = angular.module('DataAnalysisController', ['ui.router','itfreer','waterCommon','DataAnalysisServiceModule']);
	
	//趋势数据
	DataAnalysisController.controller('DataAnalysisTrendController',function($scope,$waterDataCommon,$httpData,$queryObj,$wdp,$rootScope,$state,$DataAnalysisService){
		//初始化日期
		$scope.initQuery = function(dataType){
			$scope.dataType = dataType;
			if($scope.dataType == "day"){
				$scope.numLimit=13;
			}
			else if($scope.dataType == "month"){
				$scope.numLimit=7;
			}
			// 日查询条件
			if(dataType == "day"){
				var nowDate = new Date();
				$scope.daytime=nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+"-"+nowDate.getDate();
			}
			// 周查询条件，其中年份是公用的
			$scope.yearlist=[];
			var currentYear=(new Date()).getFullYear();
			for(var i=0;i<4;i++){
				$scope.yearlist.push({
					name:currentYear-i+"年",
					value:currentYear-i+""
				});
			}; 
			$scope.selectYear=currentYear+"";
			$scope.changeYear($scope.selectYear);
			// 月查询条件
			$scope.monthlist=[];
			for(var i=1;i<=12;i++){
				$scope.monthlist.push({
					name:i+"月",
					value:""+i
				});
			};
			$scope.selectMonth=((new Date()).getMonth() + 1)+"";
			// 季度查询条件
			$scope.quarterslist=[
			                 {name:"第1季度",value:"1"},
			                 {name:"第2季度",value:"2"},
			                 {name:"第3季度",value:"3"},
			                 {name:"第4季度",value:"4"}
			                ];
			$scope.selectQuarter = ""+ (Math.floor(((new Date).getMonth()+1)/4)+1);
			
			$scope.getReport();
		}
		//年下拉数据
		$scope.year = [{"id":"2015","text":"2015年"},
		               {"id":"2016","text":"2016年"}];
		//月下拉数据
		$scope.month = [];
		for(var i=1;i<=12;i++){
			$scope.month.push({
				"id":i,"text":i+"月"
			});
		}
		
		$scope.start_year = "2015";
		$scope.end_year = "2016";
		$scope.start_month = "1";
		$scope.end_month = "12";
		
		$scope.$on('dataPramChanged', function(event, data) {
			$scope.wwtpReportParm = data;
			$scope.mn = $scope.wwtpReportParm.MN;
			$scope.parmToChecksStr = $scope.wwtpReportParm.parmToChecksStr;
			$scope.parmToCheckList = $scope.wwtpReportParm.parmToCheckList;
			$scope.getReport();
	    });
		
		$scope.stateName = $state.current.name;
		if($scope.stateName == "DataAnalysis.section_month_trend"){
			$scope.trendType = "month";
		}
		else if($scope.stateName == "DataAnalysis.section_year_trend"){
			$scope.trendType = "year";
		};

		$scope.getReport = function(){
			
			if($httpData.ReportList && $httpData.ReportList.data ){
				$httpData.ReportList.data = [];
			}
			
			var parmToChecksStr = $scope.parmToChecksStr;
			var parmToCheckList = $scope.parmToCheckList;
			
			if(!$scope.mn && $httpData.ReportList){
				$httpData.ReportList.data = [];
				return;
			}
		  
			if(parmToChecksStr == null || parmToChecksStr == ""){
				//$.messager.alert("提示","请选择要查看的参数!","info");
				return ;
			}
			
			$scope.parmToCheckList = parmToCheckList;
			
			var startDate = $scope.start_year + "-" + $scope.start_month + "-1 00:00:00";
			var endDate = $scope.end_year + "-" + $scope.end_month + "-31 23:59:59";
			var queryObj = {
					"mn":$scope.mn,
					"parmToChecks":parmToChecksStr,
					"startDate":startDate,
					"endDate":endDate,
					"trendType":$scope.trendType
			};
			
			$waterDataCommon.getPageRows("/DataAnalysis/TrendReport.do","TrendReport",queryObj,1,1,function(data){
				$("#listdata").idTabs();
			},function(){
				var data = $httpData.TrendReport.data;
				var parmToCheckList = $scope.parmToCheckList;
				$DataAnalysisService.showTrendChar(data,'tb',parmToCheckList,true);
			});
		};
		
		$scope.showSinglePollChar = function(row){
			$scope.showSingleChar = true;
			var data = [{
				report:[row],
				yearHeadText:$httpData.TrendReport.data[0].yearHeadText
			}];
			var parmToCheckList = $scope.parmToCheckList;
			$DataAnalysisService.showTrendChar(data,'float_tb',parmToCheckList);
		}
		
		$scope.closeSimglePollChar = function(){
			$scope.showSingleChar = false;
		}
		
		if($wdp.stationListReady == "true"){
			$scope.$emit('askDataPramChange', $scope.reportParms); 
		}
		
	});
	
	//同比数据
	DataAnalysisController.controller('DataAnalysisX2XController',function($scope,$waterDataCommon,$httpData,$queryObj,$wdp,$rootScope,$state,$DataAnalysisService){
		
		//年下拉数据
		$scope.year = [{"id":"2015","text":"2015年"},
		               {"id":"2016","text":"2016年"}];
		//月下拉数据
		$scope.month = [];
		for(var i=1;i<=12;i++){
			$scope.month.push({
				"id":i+"月","text":i+"月"
			});
		}
		
		$scope.yearLengths = [{"id":3,"text":"3年"},{"id":5,"text":"5年"},{"id":10,"text":"10年"}];
		$scope.yearLength = 3;
		
		$scope.showSingleChar = false;
		
		//季度下拉数据
		$scope.quarter = [];
		for(var i=1;i<=4;i++){
			$scope.quarter.push({
				"id":i+"季","text":i+"季"
			});
		}
		
		$scope.start_year = "2015";
		
		$scope._isDoubleNum = function(rowNum){
			$scope.rowNum = rowNum;
			if(rowNum % 2 == 0){
				return true;
			}
			else{
				return false;
			}
		}
		
		$scope.$on('dataPramChanged', function(event, data) {
			$scope.wwtpReportParm = data;
			$scope.mn = $scope.wwtpReportParm.MN;
			$scope.parmToChecksStr = $scope.wwtpReportParm.parmToChecksStr;
			$scope.parmToCheckList = $scope.wwtpReportParm.parmToCheckList;
			$scope.getReport();
	    });
		
		$scope.stateName = $state.current.name;
		if($scope.stateName == "DataAnalysis.section_month_to_month"){
			$scope.trendType = "month";
			$scope.mq = "1月";
		}
		else if($scope.stateName == "DataAnalysis.section_year_to_year"){
			$scope.trendType = "year";
		}
		else if($scope.stateName == "DataAnalysis.section_quarter_to_quarter"){
			$scope.trendType = "quarter";
			$scope.mq = "1季";
		};

		$scope.getReport = function(){
			
			if($httpData.ReportList && $httpData.ReportList.data ){
				$httpData.ReportList.data = [];
			}
			
			var parmToChecksStr = $scope.parmToChecksStr;
			var parmToCheckList = $scope.parmToCheckList;
			
			if(!$scope.mn && $httpData.ReportList){
				$httpData.ReportList.data = [];
				return;
			}
		  
			if(parmToChecksStr == null || parmToChecksStr == ""){
				//$.messager.alert("提示","请选择要查看的参数!","info");
				return ;
			}
			
			$scope.parmToCheckList = parmToCheckList;
			
			var queryObj = {
					"mn":$scope.mn,
					"parmToChecks":parmToChecksStr,
					"startYear":$scope.start_year,
					"mq":$scope.mq,
					"yearLength":$scope.yearLength,
					"trendType":$scope.trendType
			};
			
			$waterDataCommon.getPageRows("/DataAnalysis/X2XReport.do","TrendReport",queryObj,1,1,function(data){
				$("#listdata").idTabs();
			},function(){
				var data = $httpData.TrendReport.data;
				var parmToCheckList = $scope.parmToCheckList;
				$DataAnalysisService.showX2XChar(data,'tb',parmToCheckList);
			});
		};
		
		$scope.showSinglePollChar = function(row){
			$scope.showSingleChar = true;
			var data = [{
				report:[row],
				yearHeadText:$httpData.TrendReport.data[0].yearHeadText
			}];
			var parmToCheckList = $scope.parmToCheckList;
			$DataAnalysisService.showX2XChar(data,'float_tb',parmToCheckList);
		}
		
		$scope.closeSimglePollChar = function(){
			$scope.showSingleChar = false;
		}
		
		if($wdp.stationListReady == "true"){
			$scope.$emit('askDataPramChange', $scope.reportParms); 
		}
	});
	
	//导航控制器
	DataAnalysisController.controller('DataAnalysisNavController',function($scope,$waterDataCommon,$httpData,$queryObj,$wdp,$rootScope,$state){
		
		if($wdp.viewValue.stateWatch){
			$wdp.viewValue.stateWatch();
		}
		$wdp.viewValue.stateWatch = $rootScope.$watch(function(){return $state.current;},function(){
			$scope.stateName = $state.current.name;
        },true);
	});
}(window, angular));	